<?php
/*
setting language

file version 
*/
class lang{
/*--------------------------------------------------------------------------------------------------
                                                                                         constructor
--------------------------------------------------------------------------------------------------*/
	function lang() {
		$this->setConfig();
	}
/*--------------------------------------------------------------------------------------------------
                                                                                      public methods
--------------------------------------------------------------------------------------------------*/
	/*
	*/
	function set($p=array()) {
		// echo $default;
		if ($lang=wakGetVarIn('lang','get_post')) { // lang has been manually specified
			// registering lang in a cookie
			$lifetime=isset($this->cfg['cookie_lifetime'])?$this->cfg['cookie_lifetime']:7200;
			$expire=time()+$lifetime;
			$path=isset($this->cfg['cookie_path'])?$this->cfg['cookie_path']:'/';
			setCookie('lang',$lang,$expire,$path);
			// echo "setCookie('lang',$lang,$expire,$path);";
		} else if ($lang=wakGetVarIn('lang','cookie')) { // lang pref has been registered
			// all is great, nothing to do
		} else { // searching for browser setting
			// searching for browser accepted languages,
			// searching default amongst proposed languages
			$accepted=$this->parseHttpAcceptLanguage(); // echo wakaffvar($accepted);
			$default=NULL;
			foreach ($accepted as $item) {
				if (in_array($item['code'],$this->cfg['proposed_languages'])) {
					$lang=$item['code'];
					break;
				}
			}
		}
		if (!isset($lang))
			$lang=$this->cfg['default'];
		// echo $lang;
		define('LANG',$lang);
		// echo '<p>LANG is '.LANG.'</p>';
		return $lang;
	}
/*--------------------------------------------------------------------------------------------------
                                                                                          properties
--------------------------------------------------------------------------------------------------*/
	// config
	var $cfg=array(
		'default'=>'fr'
		, 'proposed_languages'=>array('en','fr')
		, 'cookie_lifetime'=>604800
		, 'cookie_path'=>'/'
		, 'config_file'=>NULL
	);
/*--------------------------------------------------------------------------------------------------
                                                                                     private methods
--------------------------------------------------------------------------------------------------*/
	/*
	*/
	function parseHttpAcceptLanguage() {
		// getting http instruction
		$str=$_SERVER['HTTP_ACCEPT_LANGUAGE'];
		// exploding accepted languages 
		$langs=explode(',',$str);
		// creating output list
		$accepted=array();
		foreach ($langs as $lang) {
			// parsing language preference instructions
			// 2_digit_code[-longer_code][;q=coefficient]
			ereg('([a-z]{1,2})(-([a-z0-9]+))?(;q=([0-9\.]+))?',$lang,$found);
			// 2 digit lang code
			$code=$found[1];
			// lang code complement
			$morecode=$found[3];
			// full lang code
			$fullcode=$morecode?$code.'-'.$morecode:$code;
			// coefficient
			$coef=sprintf('%3.1f',$found[5]?$found[5]:'1');
			// for sorting by coefficient
			$key=$coef.'-'.$code;
			// adding 
			$accepted[$key]=array('code'=>$code,'coef'=>$coef,'morecode'=>$morecode,'fullcode'=>$fullcode);
		}
		// sorting the list by coefficient desc
		krsort($accepted);
		return $accepted;
	}
	
	function setConfig($cfg=NULL) {
		if (!is_array($cfg)) {
			if (isset($this->config_file))
				$iniFile=$this->config_file;
			else
				$iniFile='cfg/'.get_class($this).'.config.ini';
			// loading ini file
			if (file_exists($iniFile)) {
				$cfg=parse_ini_file($iniFile,true);// echo wakaffvar($p);
			}
		}
		// merging it with default parameters
		if ($cfg) $this->cfg=wakArrayMergeRR($this->cfg, $cfg);
		return true;
	}
}
?>
